package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.ClearCartItemRequest;
import com.mall.shopping.dto.ClearCartItemResponse;
import jdk.nashorn.internal.runtime.GlobalConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ciggar
 * create-date: 2019/8/1-下午5:05
 * 将购物车中的缓存失效
 */
@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {
//    @Reference(check = false)
//    private ICartService cartService;

    @Autowired
    private RedissonClient client;

    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // log.info("begin - ClearCartItemHandler-context:" + context);
        //TODO 缓存失效和下单是属于两个事物操作，需要保证成功，后续可以改造成消息队列的形式来实现
        ClearCartItemRequest request = new ClearCartItemRequest();
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        RMap<Object, Object> map = client.getMap(createOrderContext.getUserId().toString());
        List<Long> buyProductIds = createOrderContext.getBuyProductIds();
        for (Long buyProductId : buyProductIds) {
            if (map.containsKey(buyProductId.toString())){
                map.remove(buyProductId.toString());
            }
        }
        log.info("清空购物车表完成");
        return true;

//        request.setProductIds(createOrderContext.getBuyProductIds());
//        request.setUserId(createOrderContext.getUserId());
//        ClearCartItemResponse response = cartService.clearCartItemByUserID(request);
//        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
//            log.info("清空购物车表完成");
//            return true;
//        } else {
//            throw new BizException(response.getCode(), response.getMsg());
//        }
    }
}
